home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Net / Whois.php < prev   
PHP Script  |  2004-03-24  |  9KB  |  271 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // | Portions Copyright (c) 1980, 1993 The Regents of the University of   |
  8. // |   California.  All rights reserved.                                  |
  9. // +----------------------------------------------------------------------+
  10. // | This source file is subject to version 2.02 of the PHP license,      |
  11. // | that is bundled with this package in the file LICENSE, and is        |
  12. // | available at through the world-wide-web at                           |
  13. // | http://www.php.net/license/2_02.txt.                                 |
  14. // | If you did not receive a copy of the PHP license and are unable to   |
  15. // | obtain it through the world-wide-web, please send a note to          |
  16. // | license@php.net so we can mail you a copy immediately.               |
  17. // +----------------------------------------------------------------------+
  18. // | Author: Seamus Venasse <seamus.venasse@polaris.ca>                   |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Whois.php,v 1.11 2003/01/04 11:55:54 mj Exp $
  22. //
  23. // Whois Class
  24. //
  25. require_once('PEAR.php');
  26.  
  27. /**
  28.  * Looks up records in the databases maintained by several Network Information
  29.  * Centres (NICs).  This class uses PEAR's Net_Socket:: class.
  30.  *
  31.  * @version 1.0
  32.  * @author Seamus Venasse <seamus.venasse@polaris.ca>
  33.  * @package Net
  34.  * @access public
  35.  */
  36. class Net_Whois extends PEAR {
  37.  
  38.     // {{{ properties
  39.  
  40.     /**
  41.      * List of NICs to query
  42.      *
  43.      * @var array
  44.      * @access private
  45.      */
  46.     var $_nicServers = array (
  47.         "NICHOST"           => "whois.crsnic.net",
  48.         "INICHOST"          => "whois.networksolutions.com",
  49.         "DNICHOST"          => "whois.nic.mil",
  50.         "GNICHOST"          => "whois.nic.gov",
  51.         "ANICHOST"          => "whois.arin.net",
  52.         "RNICHOST"          => "whois.ripe.net",
  53.         "PNICHOST"          => "whois.apnic.net",
  54.         "RUNICHOST"         => "whois.ripn.net",
  55.         "MNICHOST"          => "whois.ra.net",
  56.         "QNICHOST_TAIL"     => ".whois-servers.net",
  57.         "SNICHOST"          => "whois.6bone.net",
  58.         "BNICHOST"          => "whois.registro.br"
  59.     );
  60.  
  61.     /**
  62.      * Search string of server to search on
  63.      *
  64.      * @var string
  65.      * @access private
  66.      */
  67.     var $_whoisServerID = "Whois Server: ";
  68.  
  69.     /**
  70.      * Server to search for IP address lookups
  71.      *
  72.      * @var array
  73.      * @access private
  74.      */
  75.     var $_ipNicServers = array ("RNICHOST", "PNICHOST", "BNICHOST");
  76.  
  77.     /**
  78.      * List of error codes and text
  79.      *
  80.      * @var array
  81.      * @access private
  82.      */
  83.     var $_errorCodes = array (
  84.         010 => 'Unable to create a socket object',
  85.         011 => 'Unable to open socket',
  86.         012 => 'Write to socket failed',
  87.         013 => 'Read from socket failed'
  88.     );
  89.     // }}}
  90.  
  91.     // {{{ constructor
  92.     /**
  93.      * Constructs a new Net_Whois object
  94.      *
  95.      * @access public
  96.      */
  97.     function Net_Whois() {
  98.         $this->PEAR();
  99.     }
  100.     // }}}
  101.  
  102.     // {{{ query()
  103.     /**
  104.      * Connect to the necessary servers to perform a domain whois query.  Prefix
  105.      * queries with a "!" to lookup information in InterNIC handle database.
  106.      * Add a "-arin" suffix to queries to lookup information in ARIN handle
  107.      * database.
  108.      *
  109.      * @param $domain string IP address or host name
  110.      * @param $userWhoisServer string server to query (optional)
  111.      * @access public
  112.      * @return mixed returns a PEAR_Error on failure, or a string on success
  113.      */
  114.     function query($domain, $userWhoisServer = null) {
  115.         $domain = trim($domain);
  116.  
  117.         if (isset($userWhoisServer)) {
  118.             $whoisServer = $userWhoisServer;
  119.         } elseif (preg_match("/^!.*/", $domain)) {
  120.             $whoisServer = $this->_nicServers["INICHOST"];
  121.         } elseif (preg_match("/.*?-arin/i", $domain)) {
  122.             $whoisServer = $this->_nicServers["ANICHOST"];
  123.         } elseif (preg_match('/\.gov$/i', $domain)) {
  124.             $whoisServer = $this->_nicServers["GNICHOST"];
  125.         } elseif (preg_match('/\.mil$/i', $domain)) {
  126.             $whoisServer = $this->_nicServers["DNICHOST"];
  127.         } else {
  128.             $whoisServer = $this->_chooseServer($domain);
  129.         }
  130.  
  131.         $whoisData = $this->_connect($whoisServer, $domain);
  132.         if (PEAR::isError($whoisData)) {
  133.             return $whoisData;
  134.         }
  135.  
  136.         return $whoisData;
  137.     }
  138.     // }}}
  139.  
  140.     // {{{ queryAPNIC()
  141.     /**
  142.      * Use the Asia/Pacific Network Information Center (APNIC) database.
  143.      * It contains network numbers used in East Asia, Australia, New
  144.      * Zealand, and the Pacific islands.
  145.      *
  146.      * @param $ipAddress string IP address
  147.      * @access public
  148.      * @return mixed returns a PEAR_Error on failure, or a string on success
  149.      */
  150.     function queryAPNIC($domain) {
  151.         return $this->query($domain, $this->_nicServers["PNICHOST"]);
  152.     }
  153.     // }}}
  154.  
  155.     // {{{ queryIPv6()
  156.     /**
  157.      * Use the IPv6 Resource Center (6bone) database.  It contains network
  158.      * names and addresses for the IPv6 network.
  159.      *
  160.      * @param $domain string IP address or host name
  161.      * @access public
  162.      * @return mixed returns a PEAR_Error on failure, or a string on success
  163.      */
  164.     function queryIPv6($domain) {
  165.         return $this->query($domain, $thiis->_nicServers["SNICHOST"]);
  166.     }
  167.     // }}}
  168.  
  169.     // {{{ queryRADB()
  170.     /**
  171.      * Use the Route Arbiter Database (RADB) database.  It contains
  172.      * route policy specifications for a large number of operators'
  173.      * networks.
  174.      *
  175.      * @param $ipAddress string IP address
  176.      * @access public
  177.      * @return mixed returns a PEAR_Error on failure, or a string on success
  178.      */
  179.     function queryRADB($ipAddress) {
  180.         return $this->query($ipAddress, $this->_nicServers["MNICHOST"]);
  181.     }
  182.     // }}}
  183.  
  184.     // {{{ _chooseServer()
  185.     /**
  186.      * Determines the correct server to connect to based upon the domin
  187.      *
  188.      * @param $domain string IP address or host name
  189.      * @access private
  190.      * @return string whois server host name
  191.      */
  192.     function _chooseServer($domain) {
  193.         if (!strpos($domain, ".")) {
  194.             return $this->_nicServers["NICHOST"];
  195.         }
  196.  
  197.         $pieces = explode(".", $domain);
  198.         $TLD = $pieces[count($pieces)-1];
  199.         if (is_numeric($TLD)) {
  200.             $whoisServer = $this->_nicServers["ANICHOST"];
  201.         } else {
  202.             $whoisServer = $TLD . $this->_nicServers["QNICHOST_TAIL"];
  203.         }
  204.  
  205.         return $whoisServer;
  206.     }
  207.     // }}}
  208.  
  209.     // {{{ _connect()
  210.     /**
  211.      * Connects to the whois server and retrieves domain information
  212.      *
  213.      * @param $nicServer string FQDN of whois server to query
  214.      * @param $domain string domain name to query
  215.      * @access private
  216.      * @return mixed returns a PEAR_Error on failure, string of whois data on success
  217.      */
  218.     function _connect($nicServer, $domain) {
  219.         include_once 'Net/Socket.php';
  220.  
  221.         if (PEAR::isError($socket = new Net_Socket())) {
  222.             return new PEAR_Error($this->_errorCodes[010]);
  223.         }
  224.         if (PEAR::isError($socket->connect($nicServer, getservbyname('whois', 'tcp')))) {
  225.             return new PEAR_Error($this->_errorCodes[011]);
  226.         }
  227.         $socket->setBlocking(false);
  228.         if (PEAR::isError($socket->writeLine($domain))) {
  229.             return new PEAR_Error($this->_errorCodes[012]);
  230.         }
  231.  
  232.         $nHost = null;
  233.         $whoisData = $socket->readAll();
  234.         if (PEAR::isError($whoisData)) {
  235.             return new PEAR_Error($this->_errorCodes[013]);
  236.         }
  237.  
  238.         $data = explode("\n", $whoisData);
  239.         foreach ($data as $line) {
  240.             $line = rtrim($line);
  241.  
  242.             // check for whois server redirection
  243.             if (!isset($nHost)) {
  244.                 if (preg_match("/" . $this->_whoisServerID . "(.*)/", $line, $matches)) {
  245.                     $nHost = $matches[1];
  246.                 } elseif ($nicServer == $this->_nicServers["ANICHOST"]) {
  247.                     foreach ($this->_ipNicServers as $ipNicServer) {
  248.                         if (strstr($line, $ipNicServer)) {
  249.                             $nHost = $ipNicServer;
  250.                         }
  251.                     }
  252.                 }
  253.             }
  254.         }
  255.  
  256.         // this should fail, but we'll call it anyway and ignore the error
  257.         $socket->disconnect();
  258.  
  259.         if ($nHost) {
  260.             $tmpBuffer = $this->_connect($nHost, $domain);
  261.             if (PEAR::isError($tmpBuffer)) {
  262.                 return $tmpBuffer;
  263.             }
  264.             $whoisData .= $tmpBuffer;
  265.         }
  266.  
  267.         return $whoisData;
  268.     }
  269.     // }}}
  270. }
  271. ?>